<?php
/**
 * Created by PhpStorm.
 * User: jacckzhang
 * Date: 2018/9/17
 * Time: 16:18
 */
namespace app\index\controller;

use app\api\logic\TripLogic;
use app\inter\logic\OrderLogic;
use think\Controller;
use think\Db;
use think\Validate;

class Local extends Controller
{
	//司机当前位置
	public function local()
	{
		$rule = array(
			'driver_id'=>'require|number',
			'auth_type'=>'require|number',
		);
		$msg = array(
			'driver_id.require'=>'缺少司机ID参数',
			'driver_id.number'=>'司机ID类型参数错误',
			'auth_type.require'=>'缺少司机认证类型参数',
			'auth_type.number'=>'认证类型参数错误',
		);
		$validate = new Validate($rule,$msg);
		if(!$validate->check(input('get.'))){
			$tip = $validate->getError();
			return "<script  language='javascript'>alert('$tip');</script>";
		}
		$config['driver_id'] = input('get.driver_id');
		$config['auth_type'] = input('get.auth_type');
		$config['url'] = url('getLocal',$config);
		$config['protocol'] = PROTOCOL;
		$config['gd_key'] = config('GD_WEB');
		$config['images'] = json_encode(array(1=>OSS.'/public/images/car_special.png',2=>OSS.'/public/images/car_taxi.png'));
		$this->assign($config);
		return view('local');
	}

	public function getLocal()
	{
		$rule = array(
			'driver_id'=>'require|number',
			'auth_type'=>'require|number',
		);
		$msg = array(
			'driver_id.require'=>'缺少司机ID参数',
			'driver_id.number'=>'司机ID类型参数错误',
			'auth_type.require'=>'缺少司机认证类型参数',
			'auth_type.number'=>'认证类型参数错误',
		);
		$validate = new Validate($rule,$msg);
		if(!$validate->check(input('get.'))){
			return json(array('status'=>0,'msg'=>$validate->getError()));
		}
		$map['driverId'] = (int)input('get.driver_id');
		$map['authType'] = (int)input('get.auth_type');
		$res = Db::connect(config('mongodb'))->table('live_driver_location')->where($map)->find();
		if(empty($res)){
			return json(array('status'=>0,'msg'=>'该司机暂无位置信息'));
		}
		$res['img'] = 1;
		if($map['authType'] == 2){
			$res['img'] = 2;
		}
		switch ($map['authType']){
			case 1:
			case 2:
				switch ($res['driverStatus']){
					case 1:
						$res['tip'] = "司机接单中";
						break;
					case 2:
					case 3:
					case 4:
					$res['tip'] = "司机行程中";
					break;
					default:
						$res['tip'] = "司机休息中";
						break;
				}
				break;
			case 3:
				switch ($res['driverStatus']){
					case 2:
						$res['tip'] = "司机接单中";
						break;
					case 1:
					case 3:
						$res['tip'] = "司机行程中";
						break;
					default:
						$res['tip'] = "司机休息中";
						break;
				}
			break;
			case 4:
				switch ($res['driverStatus']){
					case 0:
						$res['tip'] = "司机空闲中";
						break;
					case 1:
						$res['tip'] = "司机接单中";
						break;
					case 2:
						$res['tip'] = "司机行程中";
						break;
				}
			break;
		}
		return json(array('status'=>1,'data'=>$res));
	}

	//订单轨迹
	public function trajectory()
	{
		$rule = array(
			'common_id'=>'require|number',
			'order_type'=>'require|in:1,2',
		);
		$msg = array(
			'common_id.require'=>'缺少订单ID参数',
			'common_id.number'=>'订单ID类型参数错误',
			'order_type.require'=>'缺少订单类型参数',
			'order_type.in'=>'订单类型参数错误',
		);
		$validate = new Validate($rule,$msg);
		if(!$validate->check(input('get.'))){
			$tip = $validate->getError();
			return "<script  language='javascript'>alert('$tip');</script>";
		}
		switch (input('get.order_type')){
			//城内
			case 1:
				$config = $this->inner(input('get.common_id'));
				break;
			case 2:
				$config = $this->inter(input('get.common_id'));
				break;
			default:$config = false;break;
		}
		if(empty($config)){
			return "<script  language='javascript'>alert('没有该订单的行程轨迹');</script>";
		}
		$this->assign($config);
		return view();
	}

	//城内订单轨迹
	private function inner($common_id)
	{
		$order_logic = new TripLogic();
		$order_info = $order_logic->getTripById($common_id,'local_table');
		if($order_info['local_table']){//longitude,latitude
			$local_info = Db::name($order_info['local_table'])->where('order_id',$common_id)->field('longitude,latitude')->select();
			if($local_info){
				$index = 0;
				foreach ($local_info as $key => $val){
					if($val['longitude']=='0.0' || $val['longitude']=='0.0'){
						continue;
					}
					$data[$index][] = $val['longitude'];
					$data[$index][] = $val['latitude'];
					$index++;
				}
				$config['protocol'] = PROTOCOL;
				$config['center'] = json_encode($data[0]);
				$config['local'] = json_encode($data);
				return $config;
			}
		}
		return false;
	}

	private function inter($common_id)
	{
		$order_logic = new OrderLogic();
		$order_info = $order_logic->getOrderCommonById($common_id,'local_table');
		if($order_info['local_table']){
			$local_info = Db::table($order_info['local_table'])->where('common_id',$common_id)->field('longitude,latitude')->select();
			if($local_info){
				$index = 0;
				foreach ($local_info as $key => $val){
					if($val['longitude']=='0.0' || $val['longitude']=='0.0'){
						continue;
					}
					$data[$index][] = $val['longitude'];
					$data[$index][] = $val['latitude'];
					$index++;
				}
				$config['protocol'] = PROTOCOL;
				$config['center'] = json_encode($data[0]);
				$config['local'] = json_encode($data);
				return $config;
			}
		}
		return false;
	}

	//登录地点
	public function address()
	{
		$center = trim(input('get.center'));
		$center = explode(',',$center);
		if(empty($center) || count($center)!=2){
			return "<script  language='javascript'>alert('缺少坐标点');</script>";
		}
		$config['local'] = json_encode($center);
		$config['protocol'] = PROTOCOL;
		$this->assign($config);
		return view();
	}
	//下单日志地点
	public function orderLog()
	{
		$local = input('get.');
		if(empty($local) || count($local)!=6){
			return "<script  language='javascript'>alert('参数异常');</script>";
		}
		foreach ($local as $key => $val){
			$local[$key] = trim($val);
			if(strpos($key,'local')!==false){
				$local[$key] = explode(',',trim($val));
				if(count($local[$key])!=2)unset($local[$key]);
				$local[$key] = json_encode($local[$key]);
			}
		}
		$local['protocol'] = PROTOCOL;
		$this->assign($local);
		return view();
	}
	//结算日志地点
	public function payLog()
	{
		$local = input('get.');
		if(empty($local) || count($local)!=5){
			return "<script  language='javascript'>alert('参数异常');</script>";
		}
		foreach ($local as $key => $val){
			$local[$key] = trim($val);
			if(strpos($key,'local')!==false){
				$local[$key] = explode(',',trim($val));
				if(count($local[$key])!=2)unset($local[$key]);
				$local[$key] = json_encode($local[$key]);
			}
		}

		$local['protocol'] = PROTOCOL;
		$this->assign($local);
		return view();
	}
	//报警地点
	public function alarm()
	{
		$rule = array(
			'common_id'=>'require|number',
			'order_type'=>'require|number',
		);
		$msg = array(
			'common_id.require'=>'缺少订单ID参数',
			'common_id.number'=>'订单ID类型参数错误',
			'order_type.require'=>'缺少订单类型参数',
			'order_type.number'=>'订单类型参数错误',
		);
		$validate = new Validate($rule,$msg);
		if(!$validate->check(input('get.'))){
			$tip = $validate->getError();
			return "<script  language='javascript'>alert('$tip');</script>";
		}
		switch (input('get.order_type')){
			case 1:
			case 2:
				$map['a.common_id'] = input('get.common_id');
				$config = Db::name('order_common oc')->join('driver d','oc.driver_id=d.driver_id','LEFT')
				->join('alarm a','a.common_id=oc.common_id','LEFT')->where($map)
				->field('d.driver_id,d.auth_type,start_local,start_name,arrive_local,arrive_name,alarm_local,alarm_address')->find();
				$config = decode_array($config);
			break;
			default:
				$con = config('intercity_db');
				$map['id'] = input('get.common_id');
				$config=Db::connect($con)->name('order_common oc')->join('driver d','oc.driver_id=d.id','LEFT')
					->join('alarm a','a.common_id=oc.common_id','LEFT')->where($map)
					->where($map)->field('oc.driver_id,d.auth_type,start_local,start_name,arrive_local,arrive_name,alarm_local,alarm_address')->find();
				$config = decode_array_inter($config);
				break;
		}
		$config['start_local'] = json_encode(explode(',',$config['start_local']));
		$config['arrive_local'] = json_encode(explode(',',$config['arrive_local']));
		$config['alarm_local'] = json_encode(explode(',',$config['alarm_local']));
		$config['url'] = url('getLocal',$config);
		$config['protocol'] = PROTOCOL;
		$config['gd_key'] = config('GD_WEB');
		$config['images'] = json_encode(array(1=>OSS.'/public/images/car_special.png',2=>OSS.'/public/images/car_taxi.png'));
		$this->assign($config);
		return view('alarm');
	}
}
